// 公共js
/*判断是否登录*/
// 没登录执行登录
function whetherLogin() {
    if ($api.getStorage('token') == null || $api.getStorage('token') == '' || $api.getStorage('token') == undefined) {
        api.openTabLayout({
            name: 'login_win',
            url: './html/login/login_win.html',
            useWKWebView: true,
            bgColor: '#fff',
        });
        return false;
    }
    return true;
}
// 判断用户是否登录 返回布尔值
function isLogin() {
    if ($api.getStorage('token') == null || $api.getStorage('token') == '' || $api.getStorage('token') == undefined) {
        return false;
    }
    return true;
}
//获取系统配置
function configInfo() {
    $api.ajax({
        url: 'api/config/info',
        method: 'get',
    }, function (ret) {
        api.setGlobalData({
            key: 'config_info',
            value: ret.data.info,
        });
        setTimeout(function () {
            api.sendEvent({
                name: 'isShowFun',
            });
        }, 300);

        api.removeLaunchView({
            animation: {
                type: 'none',
                duration: 1500
            }
        });
    });
}
// 获取用户信息
function memberInfo() {
    $api.ajax({
        url: 'api/member/info',
        method: 'get',
    }, function (ret) {
        api.setGlobalData({
            key: 'member_info',
            value: ret.data.info,
        });
        // 用户提现配置参数
        var dataMoney = {
            distribution: ret.data.info.distribution,
            withdraw_type: ret.data.info.withdraw_type,
            withdraw_max: ret.data.info.withdraw_max
        }
        api.setGlobalData({
            key: 'system_info',
            value: dataMoney,
        });
    });
}
// 获取用户设备id
function aJpushIDFun(){
    var ajpush = api.require('ajpush');
    ajpush.getRegistrationId(function (r) {
        $api.ajax({
          	url: 'api/member/change_device_id',
          	method: 'POST',
          	data: {
            		values: {
                    device_id: r.id
            		}
          	}
        }, function (ret,err) {
        });
    });
}
// 跳转商品详情页 1淘宝1
function openGoodsDeatilFun(data,goodsType){
    if (api.frames().findIndex(item => {return item.name == "think_find"}) != -1 || api.frames().findIndex(item => {return item.name == "searchGoods_frm"}) != -1) {
        api.setFrameAttr({
            name: 'think_find',
            bgColor: "rgba(0,0,0,0)"
        });
        api.closeFrame({
            name: "think_find"
        });
        api.closeFrame({
            name: "searchGoods_frm"
        });
    }
    if(data.type == 2 || data.type == 3 || data.type == 4){  //淘礼金（2）全额返（3） 密令（4）
        if(data.is_finish == 1){  //判断活动商品是否还有库存 （1无库存，0还有库存）
            if (isLoginFun(1)) {//无库存判断用户是否登录 请求查看用户之前是否有领取过此活动商品
                $api.ajax({
                    url: 'api/goods/userIsBuy',
                    method: 'get',
                    data: {
                        values: {
                            item_id: data.item_id,
                            type: data.type,
                        }
                    },
                }, function (ret,err) {
                    if(ret.data.is_coupon){  //用户有券链接（之前有领取过）
                        api.openTabLayout({
                            name: 'activityGoodsDetail',
                            url: 'widget://html/activityGoodsDetail.html',
                            useWKWebView: true,
                            bgColor: '#fff',
                            pageParam: {
                                data: data
                            }
                        });
                    }else{  //用户没有券链接(之前未领取)
                        if(data.type == 4){ //密令商品无库存并未领取过，直接进入普通商品详情页
                            api.openTabLayout({
                                name: 'goodsDetail',
                                url: 'widget://html/goodsDetail.html',
                                useWKWebView: true,
                                bgColor: '#fff',
                                pageParam: {
                                    data: data
                                }
                            });
                        }else if(data.type == 2){  //淘礼金商品直接提示已抢光
                            api.toast({
                                msg: '该商品已抢光',
                                duration: 1000,
                                location: 'middle'
                            });
                        }
                    }
                })
            }else{
                api.toast({
                    msg: '该商品已抢光',
                    duration: 1000,
                    location: 'middle'
                });
            }
        }else{  //有库存直接跳转活动商品页面
            api.openTabLayout({
                name: 'activityGoodsDetail',
                url: 'widget://html/activityGoodsDetail.html',
                useWKWebView: true,
                bgColor: '#fff',
                pageParam: {
                    data: data
                }
            });
        }
    }else{//淘宝天猫拼多多京东（普通商品）
        if(data.user_type == 2){ //拼多多
            if(isLoginFun(1)){
                $api.ajax({
                    url: "api/goods/pdd_get_copuon_url",
                    method: 'get',
                    data: {
                        values: {
                            item_id: data.item_id,
                        }
                    },
                }, function (ret) {
                    var h5Url = encodeURIComponent(ret.data.list[0].coupon_share_url);
                    if (api.systemType == "android") {
                        location.href = "pddopen://" ;
                        api.openApp({
                            androidPkg:location.href ,
                            mimeType: 'text/html',
                            uri: location.href
                        }, function(ret, err) {
                            if (ret) {
                            } else {
                            }
                        });
                    }else if (api.systemType == "ios") {
                        var iospdd = "pddopen://";
                        api.openApp({
                            iosUrl: iospdd,
                        }, function(ret, err) {
                            if (ret) {
                            } else {
                            }
                        });
                    }
                })
            }
        }else if (data.user_type == 3) { //京东
            if(isLoginFun(1)){
                $api.ajax({
                    url: "api/goods/jd_get_copuon_url",
                    method: 'get',
                    data: {
                        values: {
                            item_id: data.item_id,
                            coupon_url: data.coupon_share_url
                        }
                    },
                }, function (ret) {
                    var jdopen = api.require('uzkJDopen');
                    jdopen.openurl({
                        url: ret.data.list[0].coupon_share_url
                    }, function(ret, err) {
                    })
                })
            }
        }else{  //淘宝天猫商品直接进入普通商品详情页
            if(goodsType == 1){  //当goodsType为1时，淘宝天猫商品直接下单购物购物，不需要进入详情页面
                if(isLoginFun(1)){ //是否登录
                    var aliBC = api.require('aliBC');
                    if(isLoginFun(2)){//是否淘宝授权 (授权直接跳转淘宝)
                        aliBC.showPageByUrl({
                            url: data.coupon_share_url + api.getGlobalData({key: 'member_info'}).relation_id,
                            backUrl: api.systemType == "ios" ? "" : "tbopen24627965"
                        }, function (ret, err) {});
                    }else{//(未授权)
                        authorizationFun()
                    }
                }
            }else{
                api.openTabLayout({
                    name: 'goodsDetail',
                    url: 'widget://html/goodsDetail.html',
                    useWKWebView: true,
                    bgColor: '#fff',
                    pageParam: {
                        data: data
                    }
                });
            }
        }
    }
}




// 打开其他平台app
function openAppFun(type){
    if(type == 1){  //淘宝
        if(api.systemType == "ios"){
            api.openApp({
                iosUrl: 'taobao://',
            }, function(ret, err) {});
        }else if(api.systemType == "android"){
            api.openApp({
                androidPkg: 'com.taobao.taobao',
                mimeType: 'text/html',
            }, function(ret, err) {});
        }
    }else if(type == 2){  //天猫
        if(api.systemType == "ios"){
            api.openApp({
                iosUrl: 'tmall://',
            }, function(ret, err) {});
        }else if(api.systemType == "android"){
            api.openApp({
                androidPkg: 'com.tmall.wireless',
                mimeType: 'text/html',
            }, function(ret, err) {});
        }
    }else if(type == 3){  //拼多多
        if(api.systemType == "ios"){
            api.openApp({
                iosUrl: 'pddopen://',
            }, function(ret, err) {});
        }else if(api.systemType == "android"){
            api.openApp({
                androidPkg: 'com.xunmeng.pinduoduo',
                mimeType: 'text/html',
            }, function(ret, err) {});
        }
    }else if(type == 4){  //京东
        if(api.systemType == "ios"){
            api.openApp({
                iosUrl: 'openapp.jdMobile://',
            }, function(ret, err) {});
        }else if(api.systemType == "android"){
            api.openApp({
                androidPkg: 'com.jingdong.app.mall',
                mimeType: 'text/html',
            }, function(ret, err) {});
        }
    }
}

// 判断用户是否登录或者是否授权 type 是1只判断是否登录
function isLoginFun(type){
    //是否登录
    if(type == 1){
        if ($api.getStorage('token')) {//登录
            return true;
        }else{ //去登陸
            api.openTabLayout({
                name: 'login_win',
                url: 'widget://html/login/login_win.html',
                useWKWebView: true,
                bgColor: '#fff',
            });
            return false;
        }
    }else if (type == 2) {
        if(api.getGlobalData({key: 'member_info'}).relation_id > 0){  //授权
            return true;
        }else{ //去授权
            return false;
        }
    }
}

// 淘宝授权
function authorizationFun(){
    var aliBC = api.require('aliBC');
    aliBC.logout(function (ret, err) {
        if (ret.status) {
            aliBC.showLogin(function (ret, err) { //用户登录授权
                if (ret) {
                    $api.ajax({
                        url: "api/member/tb_relate",
                        method: 'get',
                    }, function (ret) {
                        aliBC.showPageByUrl({
                            url: ret.data.oauth_url,
                        }, function (ret, err) {
                            if (ret) {
                                api.toast({
                                    msg: '淘宝授权成功',
                                    duration: 2000,
                                    location: 'middle'
                                });
                            } else {
                                api.toast({
                                    msg: '您已取消淘宝授权',
                                    duration: 2000,
                                    location: 'middle'
                                });
                            }
                        });
                    })
                } else {
                    api.toast({
                        msg: '淘宝授权失败，请重试',
                        duration: 2000,
                        location: 'middle'
                    });
                }
            });
        }
    });
}

// 到账倒计时(订单页面即将到账的显示)
function paymentDate(data1,data2){
    var timestamp = (new Date()).getTime();
    //当前时间戳
    timestamp = (timestamp - (data1*1000))/1000/1000;
    return parseInt(timestamp/86400)
}

// 小贴士 (常见问题)
function common_problem() {
		api.openTabLayout({
				name: 'commonProblemList',
				url: 'widget://html/me/commonProblemList_win.html',
				useWKWebView: true,
				bgColor: '#fff',
				pageParam:{
						typeId:1,
						typeName:'常见问题'
				}
		});
}
